{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Table A10 (Appendices)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Data loaded. Shape: (1599624, 94)\n",
      "   challenger  target    year  dispute  dispute2  target_resist   id  \\\n",
      "0         2.0    20.0  1945.0      0.0       0.0            NaN  1.0   \n",
      "1         2.0    20.0  1946.0      0.0       0.0            NaN  1.0   \n",
      "2         2.0    20.0  1947.0      0.0       0.0            NaN  1.0   \n",
      "3         2.0    20.0  1948.0      0.0       0.0            NaN  1.0   \n",
      "4         2.0    20.0  1949.0      0.0       0.0            NaN  1.0   \n",
      "\n",
      "   atopally_target  defense_target  milex_challenger  ...  size_t_num2  \\\n",
      "0              1.0             1.0        90000000.0  ...          0.0   \n",
      "1              0.0             0.0        45133984.0  ...          0.0   \n",
      "2              0.0             0.0        14315999.0  ...          0.0   \n",
      "3              0.0             0.0        10960998.0  ...          0.0   \n",
      "4              1.0             1.0        13503000.0  ...        238.0   \n",
      "\n",
      "   size_t_b  dispute_hh3  dispute2_hh3  dispute_hh4  dispute2_hh4  \\\n",
      "0       0.0          0.0           0.0          0.0           0.0   \n",
      "1       0.0          0.0           0.0          0.0           0.0   \n",
      "2       0.0          0.0           0.0          0.0           0.0   \n",
      "3       0.0          0.0           0.0          0.0           0.0   \n",
      "4       1.0          0.0           0.0          0.0           0.0   \n",
      "\n",
      "   mutiny_only  violent_only  size_only  duration_only  \n",
      "0          0.0           0.0        0.0            0.0  \n",
      "1          0.0           0.0        0.0            0.0  \n",
      "2          0.0           0.0        0.0            0.0  \n",
      "3          0.0           0.0        0.0            0.0  \n",
      "4          1.0           0.0        1.0            1.0  \n",
      "\n",
      "[5 rows x 94 columns]\n"
     ]
    }
   ],
   "source": [
    "# --------------------------------------------------------\n",
    "# 0) Lightweight installer if packages are missing\n",
    "# --------------------------------------------------------\n",
    "\n",
    "try:\n",
    "    import pandas as _pd\n",
    "    import matplotlib as _mpl\n",
    "    import linearmodels as _lm\n",
    "except ImportError:\n",
    "    import sys\n",
    "    import subprocess\n",
    "    subprocess.check_call(\n",
    "        [\n",
    "            sys.executable, \"-m\", \"pip\", \"install\", \"-U\",\n",
    "            \"pandas\", \"matplotlib\", \"linearmodels\"\n",
    "        ]\n",
    "    )\n",
    "\n",
    "\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt  # only if you later want plots\n",
    "from linearmodels.panel import PanelOLS\n",
    "\n",
    "# ----------------------------\n",
    "# 1) Load data\n",
    "# ----------------------------\n",
    "\n",
    "# TODO: set your correct path here\n",
    "file_path = r\"C:\\Users\\rmuhi\\OneDrive\\Desktop\\Mutiny_Conflict Paper_All\\Replication File\\jcr_mutiny_conflict_data.dta\"\n",
    "\n",
    "data = pd.read_stata(file_path)\n",
    "\n",
    "print(\"Data loaded. Shape:\", data.shape)\n",
    "print(data.head())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "After setting MultiIndex: (1599624, 94)\n",
      "['target', 'year']\n"
     ]
    }
   ],
   "source": [
    "\n",
    "# --------------------------------------------------------\n",
    "# 2) Create lag and lead of mutiny\n",
    "# --------------------------------------------------------\n",
    "\n",
    "# Ensure required keys exist\n",
    "required_keys = [\"target\", \"year\", \"mutiny_t\"]\n",
    "for col in required_keys:\n",
    "    if col not in data.columns:\n",
    "        raise KeyError(f\"Column '{col}' is missing from the dataset.\")\n",
    "\n",
    "# Sort by panel IDs and time\n",
    "data = data.sort_values([\"id\", \"year\"])\n",
    "\n",
    "# Lag and lead of mutiny_t \n",
    "data[\"l_mutiny_t\"] = data.groupby(\"id\")[\"mutiny_t\"].shift(1)\n",
    "data[\"f_mutiny_t\"] = data.groupby(\"id\")[\"mutiny_t\"].shift(-1)\n",
    "\n",
    "# --------------------------------------------------------\n",
    "# 3) Define controls and clean panel index\n",
    "# --------------------------------------------------------\n",
    "\n",
    "# Controls (edit this list if needed)\n",
    "control_vars = [\n",
    "    \"lmmilex_persol_t\",\n",
    "    \"years_since_coup\",\n",
    "    \"ucdp_civconf_t2\",\n",
    "    \"defense_target\",\n",
    "    \"capprop\",\n",
    "    \"jdem\",\n",
    "    \"majorpower\",\n",
    "    # \"cold_war\",  # uncomment if you want to include this\n",
    "]\n",
    "\n",
    "# Check that all controls exist\n",
    "for col in control_vars:\n",
    "    if col not in data.columns:\n",
    "        raise KeyError(f\"Control variable '{col}' is missing from the dataset.\")\n",
    "\n",
    "# Drop rows with missing panel identifiers\n",
    "data = data.dropna(subset=[\"target\", \"year\"]).copy()\n",
    "\n",
    "# Set MultiIndex for panel structure: entity = target, time = year\n",
    "data = data.set_index([\"target\", \"year\"])\n",
    "\n",
    "print(\"After setting MultiIndex:\", data.shape)\n",
    "print(data.index.names)\n",
    "\n",
    "\n",
    "# Make sure panel index is set (run once, before any models)\n",
    "\n",
    "if list(data.index.names) != [\"target\", \"year\"]:\n",
    "    data = data.dropna(subset=[\"target\", \"year\"]).copy()\n",
    "    data = data.set_index([\"target\", \"year\"])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "==============================\n",
      "Model: dispute ~ mutiny_t + controls + target FE + year FE\n",
      "==============================\n",
      "                          PanelOLS Estimation Summary                           \n",
      "================================================================================\n",
      "Dep. Variable:                dispute   R-squared:                        0.0011\n",
      "Estimator:                   PanelOLS   R-squared (Between):              0.1734\n",
      "No. Observations:             1337819   R-squared (Within):               0.0012\n",
      "Date:                Fri, Dec 05 2025   R-squared (Overall):              0.0018\n",
      "Time:                        05:29:35   Log-likelihood                  2.46e+06\n",
      "Cov. Estimator:             Clustered                                           \n",
      "                                        F-statistic:                      179.67\n",
      "Entities:                         179   P-value                           0.0000\n",
      "Avg Obs:                       7473.8   Distribution:               F(8,1337563)\n",
      "Min Obs:                       317.00                                           \n",
      "Max Obs:                    1.012e+04   F-statistic (robust):             8.8842\n",
      "                                        P-value                           0.0000\n",
      "Time periods:                      70   Distribution:               F(8,1337563)\n",
      "Avg Obs:                    1.911e+04                                           \n",
      "Min Obs:                       1940.0                                           \n",
      "Max Obs:                    3.079e+04                                           \n",
      "                                                                                \n",
      "                                Parameter Estimates                                 \n",
      "====================================================================================\n",
      "                  Parameter  Std. Err.     T-stat    P-value    Lower CI    Upper CI\n",
      "------------------------------------------------------------------------------------\n",
      "mutiny_t             0.0009     0.0003     2.7084     0.0068      0.0002      0.0015\n",
      "lmmilex_persol_t    -0.0001     0.0001    -1.1692     0.2423     -0.0004   9.883e-05\n",
      "years_since_coup  1.791e-05  9.552e-06     1.8746     0.0608  -8.154e-07   3.663e-05\n",
      "ucdp_civconf_t2      0.0006     0.0003     2.4784     0.0132      0.0001      0.0011\n",
      "defense_target      -0.0003     0.0002    -1.2384     0.2156     -0.0007      0.0001\n",
      "capprop              0.0029     0.0004     7.4115     0.0000      0.0021      0.0037\n",
      "jdem                -0.0003     0.0003    -1.0021     0.3163     -0.0008      0.0003\n",
      "majorpower           0.0032     0.0008     3.9426     0.0001      0.0016      0.0049\n",
      "====================================================================================\n",
      "\n",
      "F-test for Poolability: 10.378\n",
      "P-value: 0.0000\n",
      "Distribution: F(247,1337563)\n",
      "\n",
      "Included effects: Entity, Time\n",
      "LaTeX table saved to: C:\\Users\\rmuhi\\OneDrive\\Desktop\\Mutiny_Conflict Paper_All\\Replication File\\fe_target_table_m1.tex\n"
     ]
    }
   ],
   "source": [
    "# Note: Each model of the table is estimated separately due to computation intenstive memory-related issues\n",
    "\n",
    "# --------------------------------------------------------\n",
    "# 4) Model: Mutiny only (two-way FE)\n",
    "# --------------------------------------------------------\n",
    "\n",
    "control_vars = [\n",
    "    \"lmmilex_persol_t\",\n",
    "    \"years_since_coup\",\n",
    "    \"ucdp_civconf_t2\",\n",
    "    \"defense_target\",\n",
    "    \"capprop\",\n",
    "    \"jdem\",\n",
    "    \"majorpower\",\n",
    "    # \"cold_war\",  # include if you want\n",
    "]\n",
    "\n",
    "model1_vars = [\"dispute\", \"mutiny_t\"] + control_vars\n",
    "\n",
    "# Drop rows with missing variables\n",
    "model1_data = data.dropna(subset=model1_vars).copy()\n",
    "\n",
    "# Dependent and regressors\n",
    "y1 = model1_data[\"dispute\"]\n",
    "X1 = model1_data[[\"mutiny_t\"] + control_vars]\n",
    "\n",
    "# Two-way fixed effects, cluster by entity (target)\n",
    "model1 = PanelOLS(\n",
    "    dependent=y1,\n",
    "    exog=X1,\n",
    "    entity_effects=True,   # country FE\n",
    "    time_effects=True,     # year FE\n",
    ").fit(\n",
    "    cov_type=\"clustered\",\n",
    "    cluster_entity=True,   # <-- let PanelOLS cluster by 'target'\n",
    ")\n",
    "\n",
    "print(\"\\n==============================\")\n",
    "print(\"Model: dispute ~ mutiny_t + controls + target FE + year FE\")\n",
    "print(\"==============================\")\n",
    "print(model1.summary)\n",
    "\n",
    "# Full coefficient table for Model 1\n",
    "coef_table1 = pd.DataFrame({\n",
    "    \"coef\": model1.params,\n",
    "    \"std_err\": model1.std_errors,\n",
    "    \"t\": model1.tstats,\n",
    "    \"pval\": model1.pvalues,\n",
    "})\n",
    "\n",
    "# Save as LaTeX table\n",
    "latex_path = r\"C:\\Users\\rmuhi\\OneDrive\\Desktop\\Mutiny_Conflict Paper_All\\Replication File\\table_A10_m1.tex\"\n",
    "\n",
    "coef_table1.to_latex(\n",
    "    latex_path,\n",
    "    index=True,             # keep row names\n",
    "    float_format=\"%.4f\",    # format numbers\n",
    "    caption=\"Model: Mutiny and Controls with Two-Way Fixed Effects\",\n",
    "    label=\"tab:model1\"\n",
    ")\n",
    "\n",
    "print(\"LaTeX table saved to:\", latex_path)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "==============================\n",
      "Model 2: dispute ~ mutiny_t + l_mutiny_t + controls + target FE + year FE\n",
      "==============================\n",
      "                          PanelOLS Estimation Summary                           \n",
      "================================================================================\n",
      "Dep. Variable:                dispute   R-squared:                        0.0011\n",
      "Estimator:                   PanelOLS   R-squared (Between):              0.3020\n",
      "No. Observations:             1314720   R-squared (Within):               0.0011\n",
      "Date:                Fri, Dec 05 2025   R-squared (Overall):              0.0023\n",
      "Time:                        05:38:00   Log-likelihood                 2.422e+06\n",
      "Cov. Estimator:             Clustered                                           \n",
      "                                        F-statistic:                      156.45\n",
      "Entities:                         179   P-value                           0.0000\n",
      "Avg Obs:                       7344.8   Distribution:               F(9,1314464)\n",
      "Min Obs:                       316.00                                           \n",
      "Max Obs:                       9921.0   F-statistic (robust):             9.0014\n",
      "                                        P-value                           0.0000\n",
      "Time periods:                      69   Distribution:               F(9,1314464)\n",
      "Avg Obs:                    1.905e+04                                           \n",
      "Min Obs:                       2865.0                                           \n",
      "Max Obs:                     3.03e+04                                           \n",
      "                                                                                \n",
      "                                Parameter Estimates                                 \n",
      "====================================================================================\n",
      "                  Parameter  Std. Err.     T-stat    P-value    Lower CI    Upper CI\n",
      "------------------------------------------------------------------------------------\n",
      "mutiny_t             0.0007     0.0003     2.5747     0.0100      0.0002      0.0012\n",
      "l_mutiny_t           0.0002     0.0004     0.5045     0.6139     -0.0005      0.0009\n",
      "lmmilex_persol_t -8.267e-05     0.0001    -0.7211     0.4708     -0.0003      0.0001\n",
      "years_since_coup  1.754e-05  9.786e-06     1.7924     0.0731   -1.64e-06   3.672e-05\n",
      "ucdp_civconf_t2      0.0007     0.0002     2.6859     0.0072      0.0002      0.0011\n",
      "defense_target      -0.0004     0.0002    -2.1365     0.0326     -0.0007    -3.1e-05\n",
      "capprop              0.0030     0.0004     7.2736     0.0000      0.0022      0.0038\n",
      "jdem                -0.0002     0.0003    -0.8703     0.3841     -0.0008      0.0003\n",
      "majorpower           0.0031     0.0008     3.8783     0.0001      0.0015      0.0047\n",
      "====================================================================================\n",
      "\n",
      "F-test for Poolability: 10.026\n",
      "P-value: 0.0000\n",
      "Distribution: F(246,1314464)\n",
      "\n",
      "Included effects: Entity, Time\n",
      "LaTeX table saved to: C:\\Users\\rmuhi\\OneDrive\\Desktop\\Mutiny_Conflict Paper_All\\Replication File\\fe_target_table_m2.tex\n"
     ]
    }
   ],
   "source": [
    "# --------------------------------------------------------\n",
    "# 5) Model 2: Mutiny + Lag (two-way FE)\n",
    "# --------------------------------------------------------\n",
    "\n",
    "model2_vars = [\"dispute\", \"mutiny_t\", \"l_mutiny_t\"] + control_vars\n",
    "\n",
    "model2_data = data.dropna(subset=model2_vars).copy()\n",
    "\n",
    "y2 = model2_data[\"dispute\"]\n",
    "X2 = model2_data[[\"mutiny_t\", \"l_mutiny_t\"] + control_vars]\n",
    "\n",
    "model2 = PanelOLS(\n",
    "    dependent=y2,\n",
    "    exog=X2,\n",
    "    entity_effects=True,\n",
    "    time_effects=True,\n",
    ").fit(\n",
    "    cov_type=\"clustered\",\n",
    "    cluster_entity=True,   # <-- this is the correct way to cluster by entity\n",
    ")\n",
    "\n",
    "print(\"\\n==============================\")\n",
    "print(\"Model 2: dispute ~ mutiny_t + l_mutiny_t + controls + target FE + year FE\")\n",
    "print(\"==============================\")\n",
    "print(model2.summary)\n",
    "\n",
    "\n",
    "# Full coefficient table for Model 2\n",
    "coef_table2 = pd.DataFrame({\n",
    "    \"coef\": model2.params,\n",
    "    \"std_err\": model2.std_errors,   # <-- use model2, not model1\n",
    "    \"t\": model2.tstats,\n",
    "    \"pval\": model2.pvalues,\n",
    "})\n",
    "\n",
    "# Save as LaTeX table\n",
    "latex_path = r\"C:\\Users\\rmuhi\\OneDrive\\Desktop\\Mutiny_Conflict Paper_All\\Replication File\\table_A10_m2.tex\"\n",
    "\n",
    "coef_table2.to_latex(\n",
    "    latex_path,\n",
    "    index=True,             # keep row names\n",
    "    float_format=\"%.4f\",    # format numbers\n",
    "    caption=\"Model 2: Mutiny, Lag, and Controls with Two-Way Fixed Effects\",\n",
    "    label=\"tab:model2\"\n",
    ")\n",
    "\n",
    "print(\"LaTeX table saved to:\", latex_path)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "==============================\n",
      "Model 3: dispute ~ mutiny_t + f_mutiny_t + controls + target FE + year FE\n",
      "==============================\n",
      "                          PanelOLS Estimation Summary                           \n",
      "================================================================================\n",
      "Dep. Variable:                dispute   R-squared:                        0.0010\n",
      "Estimator:                   PanelOLS   R-squared (Between):              0.1723\n",
      "No. Observations:             1309481   R-squared (Within):               0.0011\n",
      "Date:                Fri, Dec 05 2025   R-squared (Overall):              0.0018\n",
      "Time:                        06:01:32   Log-likelihood                 2.405e+06\n",
      "Cov. Estimator:             Clustered                                           \n",
      "                                        F-statistic:                      151.80\n",
      "Entities:                         179   P-value                           0.0000\n",
      "Avg Obs:                       7315.5   Distribution:               F(9,1309225)\n",
      "Min Obs:                       317.00                                           \n",
      "Max Obs:                       9921.0   F-statistic (robust):             8.0475\n",
      "                                        P-value                           0.0000\n",
      "Time periods:                      69   Distribution:               F(9,1309225)\n",
      "Avg Obs:                    1.898e+04                                           \n",
      "Min Obs:                       1939.0                                           \n",
      "Max Obs:                    3.079e+04                                           \n",
      "                                                                                \n",
      "                                Parameter Estimates                                 \n",
      "====================================================================================\n",
      "                  Parameter  Std. Err.     T-stat    P-value    Lower CI    Upper CI\n",
      "------------------------------------------------------------------------------------\n",
      "mutiny_t             0.0008     0.0003     2.5727     0.0101      0.0002      0.0015\n",
      "f_mutiny_t        7.215e-05     0.0003     0.2783     0.7808     -0.0004      0.0006\n",
      "lmmilex_persol_t    -0.0001     0.0001    -1.1860     0.2356     -0.0004   9.772e-05\n",
      "years_since_coup  1.922e-05  9.703e-06     1.9807     0.0476   2.011e-07   3.824e-05\n",
      "ucdp_civconf_t2      0.0006     0.0003     2.2062     0.0274   6.552e-05      0.0011\n",
      "defense_target      -0.0002     0.0002    -1.0634     0.2876     -0.0006      0.0002\n",
      "capprop              0.0029     0.0004     7.4339     0.0000      0.0021      0.0037\n",
      "jdem                -0.0002     0.0003    -0.7896     0.4298     -0.0008      0.0003\n",
      "majorpower           0.0031     0.0008     3.8734     0.0001      0.0016      0.0047\n",
      "====================================================================================\n",
      "\n",
      "F-test for Poolability: 10.289\n",
      "P-value: 0.0000\n",
      "Distribution: F(246,1309225)\n",
      "\n",
      "Included effects: Entity, Time\n",
      "LaTeX table saved to: C:\\Users\\rmuhi\\OneDrive\\Desktop\\Mutiny_Conflict Paper_All\\Replication File\\fe_target_table_m3.tex\n"
     ]
    }
   ],
   "source": [
    "# --------------------------------------------------------\n",
    "# 6) Model 3: Mutiny + Lead (two-way FE)\n",
    "# --------------------------------------------------------\n",
    "\n",
    "model3_vars = [\"dispute\", \"mutiny_t\", \"f_mutiny_t\"] + control_vars\n",
    "\n",
    "model3_data = data.dropna(subset=model3_vars).copy()\n",
    "\n",
    "y3 = model3_data[\"dispute\"]\n",
    "X3 = model3_data[[\"mutiny_t\", \"f_mutiny_t\"] + control_vars]\n",
    "\n",
    "model3 = PanelOLS(\n",
    "    dependent=y3,\n",
    "    exog=X3,\n",
    "    entity_effects=True,\n",
    "    time_effects=True,\n",
    ").fit(\n",
    "    cov_type=\"clustered\",\n",
    "    cluster_entity=True,   # cluster by entity (target), consistent with Model 1 & 2\n",
    ")\n",
    "\n",
    "print(\"\\n==============================\")\n",
    "print(\"Model 3: dispute ~ mutiny_t + f_mutiny_t + controls + target FE + year FE\")\n",
    "print(\"==============================\")\n",
    "print(model3.summary)\n",
    "\n",
    "\n",
    "# Full coefficient table for Model 3\n",
    "coef_table3 = pd.DataFrame({\n",
    "    \"coef\": model3.params,\n",
    "    \"std_err\": model3.std_errors,\n",
    "    \"t\": model3.tstats,\n",
    "    \"pval\": model3.pvalues,\n",
    "})\n",
    "\n",
    "# Save as LaTeX table\n",
    "latex_path3 = r\"C:\\Users\\rmuhi\\OneDrive\\Desktop\\Mutiny_Conflict Paper_All\\Replication File\\table_A10_m3.tex\"\n",
    "\n",
    "coef_table3.to_latex(\n",
    "    latex_path3,\n",
    "    index=True,             # keep row names\n",
    "    float_format=\"%.4f\",    # format numbers\n",
    "    caption=\"Model 3: Mutiny, Lead, and Controls with Two-Way Fixed Effects\",\n",
    "    label=\"tab:model3\"\n",
    ")\n",
    "\n",
    "print(\"LaTeX table saved to:\", latex_path3)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "==============================\n",
      "Model 4: dispute ~ mutiny_t + l_mutiny_t + f_mutiny_t + controls + target FE + year FE\n",
      "==============================\n",
      "                          PanelOLS Estimation Summary                           \n",
      "================================================================================\n",
      "Dep. Variable:                dispute   R-squared:                        0.0010\n",
      "Estimator:                   PanelOLS   R-squared (Between):              0.3001\n",
      "No. Observations:             1286398   R-squared (Within):               0.0011\n",
      "Date:                Fri, Dec 05 2025   R-squared (Overall):              0.0023\n",
      "Time:                        06:08:18   Log-likelihood                 2.367e+06\n",
      "Cov. Estimator:             Clustered                                           \n",
      "                                        F-statistic:                      133.62\n",
      "Entities:                         179   P-value                           0.0000\n",
      "Avg Obs:                       7186.6   Distribution:              F(10,1286142)\n",
      "Min Obs:                       316.00                                           \n",
      "Max Obs:                       9720.0   F-statistic (robust):             8.1998\n",
      "                                        P-value                           0.0000\n",
      "Time periods:                      68   Distribution:              F(10,1286142)\n",
      "Avg Obs:                    1.892e+04                                           \n",
      "Min Obs:                       2865.0                                           \n",
      "Max Obs:                     3.03e+04                                           \n",
      "                                                                                \n",
      "                                Parameter Estimates                                 \n",
      "====================================================================================\n",
      "                  Parameter  Std. Err.     T-stat    P-value    Lower CI    Upper CI\n",
      "------------------------------------------------------------------------------------\n",
      "mutiny_t             0.0006     0.0003     2.4378     0.0148      0.0001      0.0011\n",
      "l_mutiny_t           0.0001     0.0004     0.3886     0.6976     -0.0006      0.0009\n",
      "f_mutiny_t        7.233e-05     0.0002     0.2982     0.7655     -0.0004      0.0005\n",
      "lmmilex_persol_t -8.546e-05     0.0001    -0.7373     0.4610     -0.0003      0.0001\n",
      "years_since_coup  1.879e-05  9.969e-06     1.8843     0.0595  -7.541e-07   3.832e-05\n",
      "ucdp_civconf_t2      0.0006     0.0003     2.3886     0.0169      0.0001      0.0011\n",
      "defense_target      -0.0003     0.0002    -1.9452     0.0518     -0.0007   2.597e-06\n",
      "capprop              0.0030     0.0004     7.2919     0.0000      0.0022      0.0038\n",
      "jdem                -0.0002     0.0003    -0.6544     0.5129     -0.0008      0.0004\n",
      "majorpower           0.0030     0.0008     3.8197     0.0001      0.0015      0.0046\n",
      "====================================================================================\n",
      "\n",
      "F-test for Poolability: 9.9226\n",
      "P-value: 0.0000\n",
      "Distribution: F(245,1286142)\n",
      "\n",
      "Included effects: Entity, Time\n",
      "LaTeX table saved to: C:\\Users\\rmuhi\\OneDrive\\Desktop\\Mutiny_Conflict Paper_All\\Replication File\\fe_target_table_m4.tex\n"
     ]
    }
   ],
   "source": [
    "# --------------------------------------------------------\n",
    "# 7) Model 4: Mutiny + Lag + Lead (two-way FE)\n",
    "# --------------------------------------------------------\n",
    "\n",
    "model4_vars = [\"dispute\", \"mutiny_t\", \"l_mutiny_t\", \"f_mutiny_t\"] + control_vars\n",
    "\n",
    "model4_data = data.dropna(subset=model4_vars).copy()\n",
    "\n",
    "y4 = model4_data[\"dispute\"]\n",
    "X4 = model4_data[[\"mutiny_t\", \"l_mutiny_t\", \"f_mutiny_t\"] + control_vars]\n",
    "\n",
    "model4 = PanelOLS(\n",
    "    dependent=y4,\n",
    "    exog=X4,\n",
    "    entity_effects=True,\n",
    "    time_effects=True,\n",
    ").fit(\n",
    "    cov_type=\"clustered\",\n",
    "    cluster_entity=True,   # cluster by entity (target)\n",
    ")\n",
    "\n",
    "print(\"\\n==============================\")\n",
    "print(\"Model 4: dispute ~ mutiny_t + l_mutiny_t + f_mutiny_t + controls + target FE + year FE\")\n",
    "print(\"==============================\")\n",
    "print(model4.summary)\n",
    "\n",
    "\n",
    "# Full coefficient table for Model 4\n",
    "coef_table4 = pd.DataFrame({\n",
    "    \"coef\": model4.params,\n",
    "    \"std_err\": model4.std_errors,\n",
    "    \"t\": model4.tstats,\n",
    "    \"pval\": model4.pvalues,\n",
    "})\n",
    "\n",
    "# Save as LaTeX table\n",
    "latex_path4 = r\"C:\\Users\\rmuhi\\OneDrive\\Desktop\\Mutiny_Conflict Paper_All\\Replication File\\table_A10_m4.tex\"\n",
    "\n",
    "coef_table4.to_latex(\n",
    "    latex_path4,\n",
    "    index=True,             # keep row names\n",
    "    float_format=\"%.4f\",    # format numbers\n",
    "    caption=\"Model 4: Mutiny, Lag, Lead, and Controls with Two-Way Fixed Effects\",\n",
    "    label=\"tab:model4\"\n",
    ")\n",
    "\n",
    "print(\"LaTeX table saved to:\", latex_path4)\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
